package com.example.sleepsta.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.sleepsta.app.SleepStaApplication;
import com.example.sleepsta.vo.DataItem;

public class DatabaseHelper extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 1;
	private static final String TABLE_NAME = "timeRecord";
	private static final String ID = "id";
	private static final String DAY = "day";
	private static final String BEGIN_TIME = "beginTime";
	private static final String END_TIME = "endTime";
	private static final String TYPE = "type";

	public DatabaseHelper(Context context) {
		super(context, TABLE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		String CREATE_DATA_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME
				+ "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + DAY
				+ " VARCHAR," + BEGIN_TIME + " VARCHAR," + END_TIME
				+ " VARCHAR," + TYPE + " INTEGER)";

		db.execSQL(CREATE_DATA_TABLE);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}

	// public void clearTableData() {
	// SQLiteDatabase db = this.getReadableDatabase();
	// db.execSQL("DROP TABLE " + TABLE_NAME);
	// }

	/**
	 * add item
	 * 
	 * @param dataItem
	 * @param type
	 */
	public void addDataItem(DataItem dataItem) {

		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(DAY, dataItem.getDay());
		if (dataItem.getType() == 0) {
			values.put(BEGIN_TIME, dataItem.getBeginTime());
		} else {
			values.put(END_TIME, dataItem.getEndTime());
		}
		values.put(TYPE, dataItem.getType());

		long insertResult = db.insert(TABLE_NAME, null, values);
		Log.e(SleepStaApplication.TAG, "addDataItem " + insertResult);
	}

	/**
	 * 根据ID来查找一条数据
	 * 
	 * @param day
	 * @return
	 */
	public DataItem getDataItem(int id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, new String[] { ID, BEGIN_TIME,
				END_TIME }, ID + "=?", new String[] { String.valueOf(id) },
				null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		DataItem item = new DataItem(Integer.parseInt(cursor.getString(0)),
				cursor.getString(1), cursor.getString(2), cursor.getString(3),
				Integer.parseInt(cursor.getString(4)));
		return item;

	}

	/**
	 * 根据day来查找一条数据
	 * 
	 * @param day
	 * @return
	 */
	public boolean isAddDataItem(String day, int type) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, new String[] { ID, BEGIN_TIME,
				END_TIME }, DAY + "=?", new String[] { day }, null, null, null);
		if (cursor != null) {
			if (cursor.moveToFirst()) {
				Log.e(SleepStaApplication.TAG, "day: "+cursor.getString(1));
				Log.e(SleepStaApplication.TAG, "beginTime: "+cursor.getString(2));
				Log.e(SleepStaApplication.TAG, "endTime: "+cursor.getString(3));
				Log.e(SleepStaApplication.TAG, "type: "+cursor.getString(4));
				return true;
//				if (day.equals(cursor.getString(1))
//						&& type == Integer.parseInt(cursor.getString(4))) {
//					return true;
//				} else {
//					return false;
//				}
			} else {
				return false;
			}

		} else {
			return false;
		}

	}

	public int updata(DataItem dataItem) {

		SQLiteDatabase db = getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(DAY, dataItem.getDay());
		if (dataItem.getType() == 0) {
			values.put(BEGIN_TIME, dataItem.getBeginTime());
		} else {
			values.put(END_TIME, dataItem.getEndTime());
		}
		int resultCode = db.update(TABLE_NAME, values, ID + "?=",
				new String[] { String.valueOf(dataItem.getID()) });
		return resultCode;
	}

	public void deleteDataItem(DataItem item) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_NAME, ID + "=?",
				new String[] { String.valueOf(item.getID()) });
		db.close();
	}

	public int getContactsCount() {
		String countQuery = "SELECT * FROM " + TABLE_NAME;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		cursor.close();
		return cursor.getCount();

	}

	public List<DataItem> getAllItem() {
		ArrayList<DataItem> dataList = new ArrayList<DataItem>();
		String selectQuery = "SELECT * FROM " + TABLE_NAME;
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		if (cursor.moveToFirst()) {
			do {
				DataItem item = new DataItem();
				item.setID(Integer.parseInt(cursor.getString(0)));
				item.setDay(cursor.getString(1));
				item.setBeginTime(cursor.getString(2));
				item.setEndTime(cursor.getString(3));
				dataList.add(item);
			} while (cursor.moveToNext());
		}
		return dataList;
	}

}
